<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="6" :sm="24">
              <a-form-item label="活动名称">
                <a-input allowClear placeholder="请输入活动名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="活动时间">
                <a-range-picker
                  format="YYYY-MM-DD HH:mm"
                  valueFormat="YYYY-MM-DD HH:mm"
                  :placeholder="['开始时间', '结束时间']"
                  v-model="queryTime"
                  @change="changeTime"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList(1)">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList(1)">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 未开始 </a-radio-button>
          <a-radio-button value="2"> 进行中 </a-radio-button>
          <a-radio-button value="3"> 已结束 </a-radio-button>
        </a-radio-group>

        <div>
          <a-button type="primary" icon="plus" @click="edit()">添加</a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll(selectedRowKeys, 2)">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>

      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          total: this.total,
          showSizeChanger: false,
          onChange: pageChange,
        }"
      >
        <div slot="status" slot-scope="status">
          <a-badge :color="getStatus(status)[1]" :text="getStatus(status)[0]" />
        </div>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="edit(record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleAll(record.id, 2)">删除</a>
              </a-menu-item>
            </a-menu>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '活动名称',
    dataIndex: 'name',
    ellipsis: true,
    width: 200,
  },
  {
    title: '成团人数',
    dataIndex: 'pt_join_num',
    sorter: (a, b) => a.pt_join_num - b.pt_join_num,
  },
  {
    title: '成团订单',
    dataIndex: 'pt_order_count',
    sorter: (a, b) => a.pt_order_count - b.pt_order_count,
  },
  {
    title: '订单金额',
    dataIndex: 'pt_total_price',
    sorter: (a, b) => a.pt_total_price - b.pt_total_price,
  },
  {
    title: '开始时间',
    dataIndex: 'start_time',
    sorter: (a, b) => Number(new Date(a.start_time)) - Number(new Date(b.start_time)),
  },
  {
    title: '结束时间',
    dataIndex: 'end_time',
    sorter: (a, b) => Number(new Date(a.end_time)) - Number(new Date(b.end_time)),
  },
  {
    title: '活动状态',
    dataIndex: 'status',
    filters: [
      { text: '未开始', value: 1 },
      { text: '进行中', value: 2 },
      { text: '已结束', value: 3 },
    ],
    onFilter: (value, record) => record.status == value,
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    fixed: 'right',
    scopedSlots: {
      customRender: 'action',
    },
  },
]

import pt from '@/api/pt'

export default {
  data() {
    return {
      columns,
      total: 0,
      queryParam: {
        start_time: '',
        end_time: '',
        name: '',
        status: '-1',
        pageSize: 10,
        pageNo: 1,
      },
      queryTime: [],
      list: [],
      selectedRowKeys: [],
    }
  },
  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '1':
            text = '未开始'
            color = '#ff0000'
            break

          case '2':
            text = '进行中'
            color = '#65C817'
            break

          case '3':
            text = '已结束'
            color = '#918383'
            break
        }
        return [text, color]
      }
    },
  },
  created() {
    this.getList()
  },

  methods: {
    changeTime(e) {
      console.log(e)
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = ''
        this.queryParam.end_time = ''
      }
    },
    // 获取数据列表
    async getList(pageNo) {
      const hide = this.$message.loading('加载中', 0)
      let formData = this.queryParam
      pageNo && (formData.pageNo = pageNo)
      const res = await pt.activityGetList(formData)
      if (res.code == 0) {
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },
    // 新增或编辑
    async edit(item) {
      this.$router.push(`/pt/edit?id=${item ? item.id : 0}`)
    },
    // 更多操作
    async handleAll(id, status) {
      if (!id.length) return
      let formData = {
        status,
      }
      if (typeof id == 'string') {
        formData.id = id
      } else {
        formData.ids = id.join(',')
      }
      await pt.activitySetStatus(formData)
      this.getList()
    },

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    pageChange(e) {
      console.log(e)
      this.queryParam.pageNo = e
      this.getList(e)
    },
  },
}
</script>

<style></style>
